package com.haois.mp.database.module.miniapp.rest;


import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.haois.mp.database.exception.ErrorException;
import com.haois.mp.database.module.remote.service.IUserService;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.haois.mp.database.module.basic.controller.BaseController;
import com.haois.mp.database.commom.constant.StatusCode;
import com.haois.mp.database.util.mp.ProduceJwt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/mini/rest/share")
public class ShareMiniRestController extends BaseController {

    private static final Logger logger = LoggerFactory
            .getLogger(ShareMiniRestController.class);

    @Autowired
    private IUserService iUserService;

    private ProduceJwt produceJwt = new ProduceJwt();


    @RequestMapping("/member/{memberNum:.+}")
    public Map shareMember(@PathVariable String memberNum, HttpServletRequest request)
            throws RuntimeException, WxErrorException, ErrorException {
        String wxCode = request.getParameter("wxCode");
        WxMaJscode2SessionResult wResult = wxMaUserService.getSessionInfo(wxCode);
        if (wResult != null) {
            logger.info("查看分享页面的用户openId为[{}]", wResult.getOpenid());
        }
        String access_token = produceJwt.getShareToken();
        Map result = iUserService.getUserInfo(access_token, memberNum, StatusCode.SHARE_OID);
        return result;
    }

}
